import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.odps.udf.UDF;
import com.aliyun.odps.utils.StringUtils;

public class SpliderHqHairGoods extends UDF {
	
	public String evaluate(String html) {
		try {
			//HttpClientAPI HttpClientAPI = new HttpClientAPI();
			//HttpResult doGet = HttpClientAPI
			//		.doGet("https://www.hqhair.com/3ina-chubby-lipstick-2.5g-various-shades/11645087.html");
			//Document document = Jsoup.parse(doGet.getBody());
			//String html = document.getElementsByClass("page-container").toString();
			JSONObject returnObj = new JSONObject();
			if (StringUtils.isNotBlank(html)) {

				Document doc = Jsoup.parse(html);
				// 获取目录 和容量大小
				Elements categoryClass = doc.getElementsByClass("breadcrumbs_container");
				if (null != categoryClass && categoryClass.size() == 1) {
					System.out.println(categoryClass.get(0).text());
					Elements liTags = categoryClass.get(0).getElementsByTag("li");
					StringBuffer sb = new StringBuffer();
					for (Element e : liTags) {
						sb.append(e.text()).append("/");
					}
					String cate = sb.substring(0, sb.lastIndexOf("/"));
					returnObj.put("category", cate);
					System.out.println(cate);
				}
				
				//获取价格
				Elements priceEle = doc.getElementsByClass("productPrice_price");
				if(null != priceEle && priceEle.size() == 1) {
					String price = priceEle.text();
					System.out.println(price);
					returnObj.put("price", price);
				}
				Elements fromPrice = doc.getElementsByClass("productPrice_fromPrice");
				if(null != fromPrice && fromPrice.size() == 1) {
					String price = fromPrice.text();
					System.out.println(price);
					returnObj.put("price", price);
				}
				
				//获取图片链接
				Elements imgEle = doc.getElementsByClass("productImageCarousel_thumbnailWrapper");
				if(null != imgEle && imgEle.size() != 0) {
					StringBuffer sb = new StringBuffer();
					for(Element imgE :imgEle) {
						String attr = imgE.getElementsByTag("img").attr("src");
						String replace = attr.replace("/130", "/1600");
						String imgHref = "https:"+replace;
						//System.out.println(imgHref);
						sb.append(imgHref).append(",");
					}
					String img = sb.substring(0, sb.lastIndexOf(","));
					System.out.println(img);
					returnObj.put("img", img);
				}
				
				//获取产品描述和详情信息
				Elements eles = doc.getElementsByClass("newYorkProductPage_breakpoint-lg_productDescription");
				if(null != eles && eles.size() == 1) {
					Elements descp = eles.get(0).getElementsByClass("productDescription_contentPropertyListItem");
					if(null != descp && descp.size() != 0) {
						for(Element e :descp) {
							String text = e.getElementsByClass("productDescription_contentPropertyHeading").text();
							//描述
							if(StringUtils.equals("Description", text)) {
								String description = e.getElementsByTag("p").text();
								System.out.println(description);
								returnObj.put("description", description);
							}
							//详情信息   品牌，路线，成分，容量
							if(StringUtils.equals("Product Details", text)) {
								Elements detailEle = e.getElementsByClass("productDescription_contentWrapper");
								if(null != detailEle && detailEle.size() != 0) {
									for(Element detail :detailEle) {
										//获取详情标题
										Elements elementsByClass = detail.getElementsByClass("productDescription_contentPropertyName");
										//获取详情对应的值
										Elements elementsByClassVal = detail.getElementsByClass("productDescription_contentPropertyValue");
										if(null != elementsByClass && elementsByClass.size() == 1) {
											String title = elementsByClass.get(0).text();
											if(title.contains("Brand")) {
												returnObj.put("brand", elementsByClassVal.get(0).text());
											}
											if(title.contains("Directions")) {
												returnObj.put("directions", elementsByClassVal.get(0).text());
											}
											if(title.contains("Ingredients")) {
												Elements pTag = elementsByClassVal.get(0).getElementsByTag("p");
												StringBuffer sb = new StringBuffer();
												for(Element p:pTag) {
													if(p.text().contains("Ingredients may vary by shade")) {
														continue;
													}
													sb.append(p.text());
												}
												System.out.println(sb.toString());
												returnObj.put("ingredients", sb.toString());
											}
											if(title.contains("Range")) {
												returnObj.put("range", elementsByClassVal.get(0).text());
											}
											if(title.contains("Volume")) {
												returnObj.put("volume:", elementsByClassVal.get(0).text());
											}
										}
									}
								}
							}
						}
					}
					
				}
				System.out.println(returnObj.toJSONString());
				return returnObj.toJSONString();

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
